#include <cstdio>
#include <cstring>

using namespace std;

int n, m, k, a[ 1001 ][ 1001 ];
long long sum[ 1001 ][ 1001 ], f[ 1001 ];
int left[ 1001 ];

void init( )
{
    int i, j;
    scanf("%d%d%d", &n, &m, &k);
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < m; j++ )
            scanf("%d", &a[ i ][ j ]);
    for ( i = 0; i < n; i++ )
    {
        sum[ i ][ 0 ] = a[ i ][ 0 ];
        for ( j = 1; j < m; j++ )
            sum[ i ][ j ] = sum[ i ][ j - 1 ] + a[ i ][ j ];
    }
    memset( left, 0, sizeof( left ) );
    memset( f, 0, sizeof( f ) );
}

int SUM( int x, int y )
{
    if ( x < 0 || y < 0 ) return 0;
    return sum[ x ][ y ];
}

void work( )
{
    int i, j, t;
    long long ans = 0;
    for ( i = 0; i < m; i++ )
    {
        for ( j = 0; j < n; j++ )
            if ( a[ j ][ i ] < k )
                left[ j ] = i + 1;
        for ( j = 0; j < n; j++ )
        {
            if ( left[ j ] == i + 1 ) { f[ j ] = 0; continue; }
            f[ j ] = sum[ j ][ i ] - SUM( j, left[ j ] - 1 );
            for ( t = j - 1; t >= 0 && left[ t ] < left[ j ]; t-- )
                f[ j ] += sum[ t ][ i ] - SUM( t, left[ j ] - 1 );
            if ( t >= 0 )
                f[ j ] += f[ t ];
            if ( ans < f[ j ] ) ans = f[ j ];
        }
    }
    printf("%I64d\n", ans);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
